package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.jsonresulte;
import cn.tedu._03vehicle.base.response.statuscode;
import cn.tedu._03vehicle.mapper.usermapper;
import cn.tedu._03vehicle.pojo.dto.login;
import cn.tedu._03vehicle.pojo.entity.user;
import cn.tedu._03vehicle.pojo.dto.userreg;
import cn.tedu._03vehicle.pojo.vo.loginvo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@Validated
@Tag(name="用户管理")
@RestController
@RequestMapping("/user")

public class controller
{
    @Autowired
     private  usermapper mapper;
    @PostMapping("/add")
    @Operation(summary = "添加用户")
    @ApiOperationSupport(order = 10)
    public jsonresulte add(@RequestBody @Validated userreg reg)
        {
            log.debug("开始添加用户"+ reg);
            String username=reg.getUsername();
            int bb=mapper.selectByuserreg(username);
            if(bb>0)
            {
                return new jsonresulte(statuscode.USERNAME_EXISTS);
            }
            user user= new user();
            BeanUtils.copyProperties(reg,user);
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            mapper.insert(user);
            return jsonresulte.success();
        }

        @PostMapping("/login")
        @Operation(summary = "登录功能")
        @ApiOperationSupport(order = 20)
        public jsonresulte login(@RequestBody login login, HttpSession session)
        {
            log.debug("登录"+ login);
            loginvo vo=mapper.selectlogin(login);
            if(vo==null)
            {
                return new jsonresulte(statuscode.USERNAME_PASSWORD_ERROR);
            }
            session.setAttribute("user",vo);
            return jsonresulte.success(vo);
        }

        @PostMapping("/current")
    @Operation(summary = "获取当前用户")
    @ApiOperationSupport(order = 30)
    public jsonresulte current(HttpSession session)
        {
            log.debug("获取当前用户");
            loginvo vo= (loginvo) session.getAttribute("user");
//            if(vo==null)
//            {
//                return new jsonresulte(statuscode.NOT_LOGIN);
//            }
            return jsonresulte.success(vo);
        }
        @GetMapping("/logout")
    @Operation(summary = "退出登录")
    @ApiOperationSupport(order = 40)
    public void logout(HttpSession session)
        {
            log.debug("退出登录");
            session.removeAttribute("user");
        }
}
